<template>
    <div class="layout-route">
        <div class="card card-search">
            <el-form :model="queryParams" ref="queryForm" v-show="showSearch" inline label-width="110px">
                <el-form-item label="导出类型" prop="status">
                    <el-select v-model="queryParams.status" placeholder="请选择导出类型" clearable size="small" style="width: 240px">
                        <el-option v-for="(dict, index) in dataTypeList" :key="index" :label="dict.datatype_name" :value="dict.data_type" />
                    </el-select>
                </el-form-item>

                <el-form-item label="数据生成状态" prop="current_status">
                    <el-select v-model="queryParams.current_status" placeholder="请选择产品状态" clearable style="width: 240px">
                        <el-option v-for="(dict, index) in CurrentStatus" :key="index" :label="dict.label" :value="dict.value"></el-option>
                    </el-select>
                </el-form-item>

                <el-form-item label="创建时间" prop="date">
                    <el-date-picker v-model="queryParams.createtime" format="yyyy-MM-dd" type="daterange" start-placeholder="开始时间" end-placeholder="结束时间" style="width: 240px"></el-date-picker>
                </el-form-item>

                <el-form-item label=" ">
                    <el-button type="primary" icon="el-icon-search" @click="handleQuery">搜索</el-button>
                    <el-button icon="el-icon-refresh" @click="resetQuery">重置</el-button>
                </el-form-item>
            </el-form>
        </div>
        <div class="card">
            <el-row :gutter="10" class="mb8">
                <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
            </el-row>
            <el-table v-loading="loading" :data="tableData" border script>
                <el-table-column label="ID" prop="id" min-width="50"></el-table-column>
                <el-table-column label="文件名称" prop="task_name"></el-table-column>
                <el-table-column label="导出类型" prop="data_type"></el-table-column>
                <el-table-column label="数据生成状态" prop="status"></el-table-column>
                <el-table-column label="导出数据条数" prop="rows_sum"></el-table-column>
                <el-table-column label="实际导出条数" prop="done_sum"></el-table-column>
                <el-table-column label="创建人" prop="created_work_no"></el-table-column>
                <el-table-column label="创建任务时间" prop="created_unix"></el-table-column>
                <el-table-column label="操作" prop="status" min-width="60" fixed="right">
                    <template slot-scope="scope">
                        <el-button type="text" icon="el-icon-download" @click="handleExport(scope.row)">下载</el-button>
                        <el-button type="text" @click="deleteItem(scope.row, scope.$index)" :disabled="loadingDelete">删除</el-button>
                    </template>
                </el-table-column>
            </el-table>
            <pagination v-show="total > 0" :total="total" :page.sync="pageInfo.page" :limit.sync="pageInfo.limit" @pagination="getList"></pagination>
        </div>
    </div>
</template>

<script>
import { queryDownloadList, getDataType,deletSystemExport } from '@/api/download'
import { parseTime } from '@/utils/quickadmin'

export default {
    name: 'SystemExport',
    components: {},
    data() {
        return {
            // 遮罩层
            loading: true,
            download: false,
            // 显示搜索条件
            showSearch: true,
            // 总条数
            total: 0,
            // 表格数据
            tableData: [],
            dataTypeList: [],
            pageInfo: {
                page: 1,
                limit: 10,
            },
            // 查询参数
            queryParams: {
                advertiser_id: undefined,
                current_status: undefined,
            },
            CurrentStatus: [{label: '待生成数据', value:0}, {label: '数据生成完成', value:3}, {label: '数据已生成部分', value:4}],
        }
    },
    watch: {
        $route: {
            handler(val) {
                this.queryParams.advertiser_id = val.query.advertiser_id
            },
            immediate: true,
        },
    },
    async created() {
        const data = await getDataType()
        this.dataTypeList = data?.data ?? []
        this.getList()
    },
    methods: {
        deleteItem(item, index){
            this.$confirm(`是否确认删除?`, '警告', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                this.loadingDelete = true
                deletSystemExport({id: item.id}).then(res=>{
                    this.$message.success(res.msg)
                    this.resetQuery()
                }).finally(()=>{
                    this.loadingDelete = false
                })
            }).catch(() => { })
        },
        handleExport(row) {
            const downloadUrl = row.path
            window.open(downloadUrl, '_blank')
        },
        //列表
        getList() {
            this.loading = true
            const qyparams = {
                ...this.pageInfo,
                ...this.queryParams,
            }
            if (qyparams.createtime?.length == 2) {
                qyparams.createtime = [parseTime(qyparams.createtime[0], 'y-m-d'), parseTime(qyparams.createtime[1], 'y-m-d')]
            }
            queryDownloadList(qyparams)
                .then(response => {
                    this.tableData = response.data.list
                    this.total = response.data.total
                })
                .finally(() => {
                    this.loading = false
                })
        },

        // 搜索按钮操作
        handleQuery() {
            this.pageInfo.page = 1
            this.getList()
        },

        // 重置按钮操作
        resetQuery() {
            this.resetForm('queryForm')

            this.handleQuery()
        },
    },
}
</script>
